package com.jdd.fintech.morse.plugin.strategy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jdd.fintech.morse.plugin.constans.OpenApiConstans;
import com.jdd.fintech.morse.plugin.ecrypts.SymmetricCrypto;
import com.jdd.fintech.morse.plugin.plugconfig.MorsePlugValueConfig;
import com.jdd.fintech.morse.plugin.strategy.context.AbstractDataEncryptAndDecryptStrategy;
import com.jdd.fintech.morse.plugin.util.CommonMethodUtils;
import com.jdd.fintech.morse.plugin.util.JsonStringUtils;
import com.jddglobal.filter.support.exception.FilterException;


/**
 * @description: 金城数据加密策略类
 * @credit: 2021-12-24 11:05
 * @author: fengcheng
 *
 **/
public class JcDataEncryptAndDecryptStrategy extends AbstractDataEncryptAndDecryptStrategy {

    /**
     * 数据加密策略接口方法(金城使用)
     * @param plugValueConfig   插件值对象
     * @param jsonObject        请求对象
     * @param scObj             算法对象
     * @return
     * @throws FilterException 方法
     */
    @Override
    public String dataEncryptsStrategyMethod(MorsePlugValueConfig plugValueConfig, JSONObject jsonObject, SymmetricCrypto scObj) throws FilterException {
        log.info("JcDataEncryptAndDecryptStrategy.dataEncryptsStrategyMethod.金城银行的加密操作与金条使用同一种逻辑，故此处不做处理！");
        return null;
    }

    /**
     * 数据解密策略接口方法(金城使用)
     * @param plugValueConfig   插件值对象
     * @param jsonObject        请求对象
     * @param scObj             算法对象
     * @return
     * @throws FilterException 方法
     */
    @Override
    public String dataDecryptsStrategyMethod(MorsePlugValueConfig plugValueConfig, JSONObject jsonObject, SymmetricCrypto scObj) throws FilterException {
        log.info("JcDataEncryptAndDecryptStrategy.buildCommonRequestObjMethod.金城银行的解密操作与金条使用同一种逻辑，故此处不做处理！");
        return null;
    }

    /**
     * 构建公共请求对象(金城使用)
     * @param plugValueConfig
     * @param tenantId
     * @param productCode
     * @param jsonObject
     * @return
     */
    @Override
    public String buildCommonRequestObjMethod(MorsePlugValueConfig plugValueConfig, String tenantId, String productCode, JSONObject jsonObject) throws FilterException {
        log.info("JcDataEncryptAndDecryptStrategy.buildCommonRequestObjMethod.金城银行---构建响应数据======>开始");
        String interfaceRequestDirection = CommonMethodUtils.getMessageBizDateKey(plugValueConfig);
        //判断是否是JSON,如果不是直接返回原始json字符串
        if(!JsonStringUtils.judgeIsJson(interfaceRequestDirection, jsonObject)){
            return jsonObject.toJSONString();
        }
        try {
            //构建响应数据
            JSONObject jsonRequestData = JSON.parseObject(jsonObject.getString(interfaceRequestDirection).trim(), JSONObject.class);
            jsonRequestData.put("tenantId", tenantId);
            jsonRequestData.put("productCode", productCode);
            jsonRequestData.put("requestNo", jsonObject.getString("requestNo"));
            jsonRequestData.put("requestTime", jsonObject.getString("requestTime"));
            jsonRequestData.put("ext1", jsonObject.getString("ext1"));
            jsonRequestData.put("ext2", jsonObject.getString("ext2"));
            jsonObject.put(interfaceRequestDirection, jsonRequestData.toJSONString());
            jsonObject.remove(OpenApiConstans.SYSSIGN);
            log.info("JcDataEncryptAndDecryptStrategy.buildCommonRequestObjMethod.金城银行---构建响应数据======>结束");
            return jsonObject.toJSONString();
        } catch (Exception e) {
            log.error("JcDataEncryptAndDecryptStrategy.buildCommonRequestObjMethod.金城|数据格式错误，转换失败|解密后的数据|jsonObject:{}|异常信息：{}", jsonObject.toJSONString(), e.getMessage(), e);
            return jsonObject.toJSONString();
        }
    }
}
